package com.lancoo.znbkxx.teacher.ui.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.base.BaseViewModel
import com.lancoo.znbkxx.teacher.databinding.ItemAnswerSkillCardBinding
import com.lancoo.znbkxx.teacher.model.data.AnswerSkillData
import com.lancoo.znbkxx.teacher.model.data.AnsweringSkill
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import com.lancoo.znbkxx.teacher.viewmodel.HardSolutionViewModel
import kotlinx.android.synthetic.main.fragment_skill_comment.*
import kotlinx.android.synthetic.main.item_answer_skill_card.*
import kotlinx.android.synthetic.main.item_tab_skill.view.*

/**
 * Created by wealive on 2021/3/12.
 *
 *
 */
class SelectAnswerSKillFragment(val changeListener: onChangeListener) :
        BaseVMFragment<HardSolutionViewModel, ItemAnswerSkillCardBinding>() {
    private val position by lazy { requireArguments().getInt("position") }
    override fun providerVMClass(): Class<HardSolutionViewModel>? {
        return HardSolutionViewModel::class.java
    }

    override fun providerContext(): Any? {
        return activity
    }

    override fun setLayoutID(): Int {
        return R.layout.item_answer_skill_card
    }

    override fun initView() {
       viewModel.skillList.value!!.forEach { parent ->
            viewModel.answerSkillList.value!!.forEach {
                if (it.KnowledgeCode == parent.KnowledgeCode) {
                    it.AnswerSkillDataList[0].isSelect = true
                }
            }
        }
        tv_title.text = viewModel.answerSkillList.value!![position].AnswerSkillDataList[0].Title
        tv_text.text = HtmlUtils.setText(
                viewModel.answerSkillList.value!![position].AnswerSkillDataList[0].MainContext)
        tv_add.setOnClickListener {
            if (viewModel.answerSkillList.value!![position].AnswerSkillDataList[0].isSelect) {
                viewModel.answerSkillList.value!![position].AnswerSkillDataList[0].isSelect = false
                tv_add.apply {
                    text = "添加"
                    setTextColor(Color.parseColor("#ffffff"))
                    setBackgroundResource(R.drawable.aibk_bg_add)
                }
            } else {
                if (getTotalAddNum() >= 3) {
                    MyToast.showToast("至多添加3个技巧讲解")
                    return@setOnClickListener
                }
                viewModel.answerSkillList.value!![position].AnswerSkillDataList[0].isSelect = true
                tv_add.apply {
                    text = "取消"
                    setTextColor(Color.parseColor("#00afff"))
                    setBackgroundResource(R.drawable.aibk_bg_cancel)
                }
            }
            changeListener.add(position,
                    viewModel.answerSkillList.value!![position].AnswerSkillDataList[0].isSelect)
        }
        if (viewModel.answerSkillList.value!![position].AnswerSkillDataList[0].isSelect) {
            tv_add.apply {
                text = "取消"
                setTextColor(Color.parseColor("#00afff"))
                setBackgroundResource(R.drawable.aibk_bg_cancel)
            }
        } else {
            tv_add.apply {
                text = "添加"
                setTextColor(Color.parseColor("#ffffff"))
                setBackgroundResource(R.drawable.aibk_bg_add)
            }
        }
    }

    interface onChangeListener {
        fun add(position: Int, isAdd: Boolean)
    }

    fun getTotalAddNum(): Int {
        var num = 0
        viewModel.answerSkillList.value!!.forEach {
            if (it.AnswerSkillDataList[0].isSelect) {
                num++
            }
        }
        return num
    }
}